#include "unit.h"
#include <QDebug>

Unit::Unit()
{
    init();
    ///dimensions par défaut :
    this->m_geometry.setTop(0);
    this->m_geometry.setLeft(0);
    this->m_geometry.setWidth(50);
    this->m_geometry.setHeight(50);
}

Unit::Unit(QRect &geometry)
{
    init();
    this->setgeometry(geometry);
}

Unit::~Unit()
{
}

QRect Unit::getGeometry()
{
    return this->m_geometry;
}

void Unit::getGeometry(int &left, int &top, int &right, int &bottom)
{
    left = m_geometry.left();
    top = m_geometry.top();
    right = m_geometry.right();
    bottom = m_geometry.bottom();
}


int Unit::getLives() {return this->m_lives;}

void Unit::setgeometry(QRect &geometry)
{
    m_geometry.setLeft(geometry.left());
    m_geometry.setTop(geometry.top());
    m_geometry.setWidth(geometry.width());
    m_geometry.setHeight(geometry.height());
}

void Unit::setgeometry(int left, int top, int width, int height)
{
    m_geometry.setLeft(left);
    m_geometry.setTop(top);
    m_geometry.setWidth(width);
    m_geometry.setHeight(height);
}

void Unit::setLives(int lives) {this->m_lives = lives;}

void Unit::init()
{
    m_step = 0;
    this->setLives(1);
}

void Unit::loseALife() {this->setLives(m_lives - 1);}

void Unit::gainALife() {this->setLives(m_lives + 1);}

bool Unit::isDead() {return (m_lives <= 0);}

bool Unit::collidesWith(Unit *unit)
{
    return(this->m_geometry.intersects(unit->getGeometry()));
}


void Unit::serializeDebug(QDebug &dbg)
{
    dbg << "Unit("
        << "Geometry:" << m_geometry
        << "Lives:"    << m_lives
        << ")";
}

void Unit::serializeOut(QDataStream &out)
{
    out << m_geometry;
    out << (qint32)m_lives;
}

void Unit::serializeIn(QDataStream &in)
{
    in >> m_geometry;
    in >> m_lives;
}

QDataStream & operator << (QDataStream &out, Unit* unit)
{
    unit->serializeOut(out);
    return out;
}

QDataStream & operator >> (QDataStream &in,  Unit* unit)
{
    unit->serializeIn(in);
    return in;
}

QDebug operator<<(QDebug dbg, Unit *unit)
{
    unit->serializeDebug(dbg);
    return dbg;
}

